import { createRouter, createWebHistory } from "vue-router";
import { message } from "ant-design-vue";
import { getStorageRole } from '../utils/index';

import { getRouters } from "./utils";

const router = createRouter({
  // createWebHashHistory (hash路由)
  // createWebHistory (history路，需要服务器配置支持)
  // createMemoryHistory 带缓存 history 路由
  // 添加baseUrl，createWebHistory(baseUrl)
  history: createWebHistory(),
  routes: getRouters(),
});

router.beforeEach((to, from, next) => {
  const role = getStorageRole();
  if (!role && to.name !== "login") {
    message.info("你还没登录呢，请进行登录");
    next({ name: "login" });
  } else {
    next();
  }
});

export function setupRouter(app) {
  app.use(router);
}

export default router;
